package com.nsgf.library.ui.page.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.MessageBean;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.data.repository.DataRepository;
import com.nsgf.library.data.response.DataResult;
import com.nsgf.library.databinding.FragmentNagfListBinding;
import com.nsgf.library.databinding.FragmentNagfManagerListBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.event.EventMsg;
import com.nsgf.library.event.RxBus;
import com.nsgf.library.ui.adapter.PlaylistAdapter;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.MessageDetailsActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class WarningMessageFragment extends BaseFragment {

    public static WarningMessageFragment newInstance(String warningType, String type) {

        WarningMessageFragment messageFragment = new WarningMessageFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        bundle.putString("warningType", warningType);
        messageFragment.setArguments(bundle);
        return messageFragment;
    }

    public static WarningMessageFragment newInstance(String type) {
        WarningMessageFragment messageFragment = new WarningMessageFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        messageFragment.setArguments(bundle);
        return messageFragment;

    }

    private String getType() {
        return getArguments().getString("type");
    }

    private String getWarningType() {
        return getArguments().getString("warningType");
    }

    private MessageFragment.MessageFragmentStates mStates;

    private PlaylistAdapter mAdapter;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(MessageFragment.MessageFragmentStates.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);

    }

    private FragmentNagfListBinding mBinding;

    private AccountRequester mAccountRequester;

    @Override
    protected DataBindingConfig getDataBindingConfig() {

        mAdapter = new PlaylistAdapter(getContext());

        return new DataBindingConfig(R.layout.fragment_nagf_list, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getLifecycle().addObserver(mAccountRequester);
        mBinding = (FragmentNagfListBinding) getBinding();


        mBinding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                getDatas(search);
            }
        });
        mAdapter.setOnItemClickListener((viewId, item, position) -> {

            Intent intent = new Intent(getActivity(), MessageDetailsActivity.class);
            intent.putExtra("title", item.getTypeName());
            intent.putExtra("id", item.getId());
            intent.putExtra("warning", true);
            startActivity(intent);
            mAdapter.getCurrentList().get(position).setIsRead(1);
        });

        mBinding.smartRefreshLayout.autoRefresh();

    }

    private String search = "";

    public void notifyDataSetChanged() {
        for (int i = 0; i < mAdapter.getCurrentList().size(); i++) {
            mAdapter.getCurrentList().get(i).setIsRead(1);
        }
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 获取数据
     *
     * @param search
     */
    public void getDatas(String search) {
        this.search = search;

        DataRepository.getInstance().getWarningMessage(getType(), search, "1".equals(getWarningType()) ? null : UserInfo.getInstance().getProjectCode())
                .subscribe(new io.reactivex.Observer<DataResult<MessageBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(DataResult<MessageBean> dataResult) {
                        if (dataResult.getResponseStatus().isSuccess()) {
                            if (dataResult.getResult().getCode() == 200) {
                                mAdapter.submitList(dataResult.getResult().getData());
                                if (dataResult.getResult().getData().size() > 0) {
                                    mBinding.notdata.setVisibility(View.GONE);
                                } else {
                                    mBinding.notdata.setVisibility(View.VISIBLE);
                                }
                            }
                        }
                        mBinding.smartRefreshLayout.finishRefresh();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
